package geral.manager;

import geral.util.FacesUtil;

import java.io.ByteArrayInputStream;
import java.io.Serializable;

import javax.ejb.EJB;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;

import seguranca.manager.SegurancaManager;
import aspecto.annotation.Permissao;
import br.com.sgc.entities.geral.Clinica;
import br.com.sgc.entities.seguranca.Usuario;
import br.com.sgc.exception.ValidacaoException;
import br.com.sgc.permissao.Role;
import br.com.sgc.session.GenericSession;

import com.encripta.EncriptaSenha;


@Named
@SessionScoped
public class ClinicaManager implements Serializable{
	
	
	private static final long serialVersionUID = 6465129932235132229L;
	
	private Clinica clinica = new Clinica();
	
	private Usuario usuario = new Usuario();
	
	private DefaultStreamedContent content;
	
	@EJB private GenericSession<Clinica> clinicaSession;
	
	@Inject private SegurancaManager segurancaManager;
	
	
	
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public String prepareSave(){
		clinica = new Clinica();
		usuario = new Usuario();
		return "primeiroAcesso";
	}
	
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public String save() {
		try {
			usuario.setSenha(EncriptaSenha.criptografa(usuario.getSenha()));
			usuario.setSuperUsuario(true);
			usuario.setClinica(clinica);
			usuario.setPerfilUsuario(null);
			clinica.getUsuariosVinculados().add(usuario);
			clinicaSession.save(clinica);
			FacesUtil.mensInfo("Salvo com Sucesso!!!");
			return "login";
		} catch (ValidacaoException e) {
			e.printStackTrace();
			FacesUtil.mensWarn(e.getMessage());
		}
		return null;
	}
	
	@Permissao(role = Role.Sistema_SGC_Clinica_Atualizar)
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public String update() {
		try {
			clinicaSession.update(clinica);
			FacesUtil.mensInfo("Atualizado com Sucesso!!!");
			return "homeSGC";
		} catch (ValidacaoException e) {
			e.printStackTrace();
			FacesUtil.mensWarn(e.getMessage());
		}
		return null;
	}
	
	@Permissao(role = Role.Sistema_SGC_Clinica_Atualizar)
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public String prepareUpdate(){
		clinica = clinicaSession.findById(Clinica.class, segurancaManager.getUsuarioLogado().getClinica().getId(), true);
		if(clinica.getLogotipo() != null)
			content = new DefaultStreamedContent(new ByteArrayInputStream(clinica.getLogotipo()));
		
		return "formClinica";
	}
	
	public void handleFileUpload(FileUploadEvent event) {
		clinica.setLogotipo(event.getFile().getContents());
		content = new DefaultStreamedContent(new ByteArrayInputStream(clinica.getLogotipo()));
    }

	public Clinica getClinica() {
		return clinica;
	}

	public void setClinica(Clinica clinica) {
		this.clinica = clinica;
	}

	public GenericSession<Clinica> getClinicaSession() {
		return clinicaSession;
	}

	public void setClinicaSession(GenericSession<Clinica> clinicaSession) {
		this.clinicaSession = clinicaSession;
	}

	public DefaultStreamedContent getContent() {
		return content;
	}

	public void setContent(DefaultStreamedContent content) {
		this.content = content;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}
	
	
}